<template>
	<view>
		<uni-list>
			<uni-list-item :title="getName"></uni-list-item>
			<uni-list-item :title="getStudnetNumber"></uni-list-item>
			<uni-list-item title="账号设置" showArrow="true" badge-text="12" clickable="true" link to="../login/login">
			</uni-list-item>
			<uni-list-item title="查询最好成绩" note="关闭则查询全部成绩" :switchChecked="scoreQueryMode" show-switch="true"
				@switchChange="onModeChange"></uni-list-item>
			<uni-list-item title="使用成绩替代查询入口" note="关闭则使用默认入口" :switchChecked="scoreQueryEntry" show-switch="true"
				@switchChange="onEntryChange"></uni-list-item>
			<uni-list-item note="点击日期校正" showArrow="true" clickable="true">
				<picker slot="body" mode="date" :value="date" @change="bindDateChange">
					<text space="emsp">校正起始周 当前{{date}}</text>
				</picker>
			</uni-list-item>
			<uni-list-item title="关于" showArrow="true" link to="../about/about" clickable="true"></uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			const setting = getApp().globalData.settingData
			const table = getApp().globalData.tableData
			return {
				scoreQueryMode: setting.scoreQueryMode === "ALL" ? false : true,
				scoreQueryEntry: setting.scoreQueryEntry === "default" ? false : true,
				date: this.getDate(new Date(setting.beginTime)),
				name: table.name,
				number: table.number,
			}
		},
		onShow() {
			console.log("settings onShow")
			let table = getApp().globalData.tableData
			this.name = table.name
			this.number = table.number
		},
		methods: {
			onEntryChange(e) {
				console.log(e)
				let setting = getApp().globalData.settingData
				setting.scoreQueryEntry = e.value === true ? "alternative" : "default"
				uni.setStorage({
					key: "setting_data_key",
					data: setting,
				})
			},
			onModeChange(e) {
				console.log(e.value)
				let setting = getApp().globalData.settingData
				setting.scoreQueryMode = e.value === true ? "MAX" : "ALL"
				uni.setStorage({
					key: "setting_data_key",
					data: setting,
				})
			},
			getDate(date) {
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			bindDateChange(e) {
				this.date = e.target.value
				let [year, month, day] = this.date.split("-")
				let date = new Date(Date.parse(`${month}/${day}/${year}`))
				console.log(date)
				let correctDate = this.getMonday(date)
				console.log(correctDate)
				let setting = getApp().globalData.settingData
				setting.beginTime = correctDate.getTime()
				uni.setStorage({
					key: "setting_data_key",
					data: setting
				})
			},
			getMonday(date) {
				let day = date.getDay();
				let deltaDay;
				if (day == 0) {
					deltaDay = 6;
				} else {
					deltaDay = day - 1;
				}
				let monday = new Date(date.getTime() - deltaDay * 24 * 60 * 60 * 1000);
				monday.setHours(0);
				monday.setMinutes(0);
				monday.setSeconds(0);
				return monday; //返回本周的周一的0时0分0秒
			}

		},
		computed: {
			getName() {
				return `姓名 ${this.name}`
			},
			getStudnetNumber() {
				return `学号 ${this.number}`
			}
		}
	}
</script>

<style>

</style>
